//
//  ChartView.m
//  IOSlineChart
//
//  Created by Apple on 16/5/10.
//  Copyright © 2016年 zhangleishan. All rights reserved.
//

#import "ChartView.h"
#import "UUChart.h"

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define OneColor [UIColor purpleColor] //链接的线条颜色
#define TwoColor UIColorFromRGB(0XD7036A)
#define YeJiColor UIColorFromRGB(0XD7063A)

@interface ChartView ()<UUChartDataSource>{
    UUChart *chartView;
    int max;  //最大值
    int min;  //最小值
    
    NSArray *X;  //横坐标
    NSArray *Y;  //纵坐标
    
    UISwitch *switchs; //开关
    BOOL isShowValue;  //显示数值
    
}

@end

@implementation ChartView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        
    isShowValue = NO;

    }
    return self;
}
-(void)setData
{
    NSMutableArray *finalY=[NSMutableArray arrayWithCapacity:0];
    [finalY addObject:_arrayY];
    [self JsX:_arrayX];
    [self JsY:finalY];
    [chartView removeFromSuperview];
    chartView = [[UUChart alloc]initwithUUChartDataFrame:CGRectMake(10, 0,self.frame.size.width-20, self.frame.size.height) withSource:self withStyle:UUChartLineStyle];
    [chartView showInView:self];
}

-(void)JsX:(NSArray *)ary{
    X = [NSArray arrayWithArray:ary];
}
-(void)JsY:(NSArray *)ary{
    NSArray * old1 = [ary objectAtIndex:0];
    max = [[old1 valueForKeyPath:@"@max.intValue"] intValue];
    min = [[old1 valueForKeyPath:@"@min.intValue"] intValue];
    Y = [NSArray arrayWithArray:ary];

}

#pragma mark------直线图和柱状图共用的代码
- (NSArray *)UUChart_xLableArray:(UUChart *)chart{  //横坐标数据
    return X;
}
- (NSArray *)UUChart_yValueArray:(UUChart *)chart{  //纵坐标数据
    return Y;
}

//颜色数组
- (NSArray *)UUChart_ColorArray:(UUChart *)chart
{
    return @[OneColor,TwoColor];  //返回颜色数组，不同的线返回不同的数组
}
//显示数值范围
- (CGRange)UUChartChooseRangeInLineChart:(UUChart *)chart
{
    return CGRangeMake(max, min);
}

#pragma mark 折线图专享功能

//标记数值区域颜色加深
- (CGRange)UUChartMarkRangeInLineChart:(UUChart *)chart{
    
    return CGRangeZero;
}

//判断显示横线条
- (BOOL)UUChart:(UUChart *)chart ShowHorizonLineAtIndex:(NSInteger)index
{
    return YES;
}

//判断显示最大最小值
- (BOOL)UUChart:(UUChart *)chart ShowMaxMinAtIndex:(NSInteger)index
{
    return NO;
}

//判断是否显示数值
-(BOOL)UUChartShowValues:(UUChart *)chart{
    return isShowValue;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
